package com.itheima.server.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.common.core.domain.AjaxResult;
import com.itheima.common.core.domain.R;
import com.itheima.server.domain.Activite;
import com.itheima.server.dto.ActiviteDto;
import com.itheima.server.service.ActiviteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/clues/activity")
@Slf4j
public class ActiviteController {
    @Autowired
    private ActiviteService activiteService;

    /**
     *  新增活动
     * @param activiteDto
     * @return
     */
    @PostMapping
    public R addActivite(@RequestBody ActiviteDto activiteDto){
        log.info("添加活动",activiteDto);
         Boolean flag = activiteService.addActivite(activiteDto);
         if (!flag){
             return R.fail("添加失败");
         }
        return R.ok();
    }


    /**
     *  查询活动列表
     * @return
     */
    @GetMapping("/list")
    public AjaxResult getActiviteList(@RequestParam Map<String, String> params) {
        log.info("查询活动列表: {}",params);
        // 获取分页参数
        Integer pageNum = Integer.parseInt(params.get("pageNum"));
        Integer pageSize = Integer.parseInt(params.get("pageSize"));

        String code = params.get("code");

        String channel = params.get("channel");

        // 获取时间参数 - 注意参数名称包含方括号
        String beginCreateTime = params.get("params[beginCreateTime]");
        String endCreateTime = params.get("params[endCreateTime]");
        String beginTime = params.get("params[beginTime]");
        String endTime = params.get("params[endTime]");
        AjaxResult ajaxResult = activiteService.getActiviteList(pageNum,pageSize,code,channel,beginCreateTime,endCreateTime,beginTime,endTime);
        return ajaxResult;
    }

    /**
     *  查询活动详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getActiviteById(@PathVariable Long id){
        log.info("查询活动详情: {}",id);
        Activite activite = activiteService.getActiviteById(id);
        return R.ok(activite);
    }

    /**
     *  修改活动
     * @param activite
     * @return
     */
    @PutMapping
    public R updateActivite(@RequestBody Activite activite){
        log.info("修改活动: {}",activite);
        Boolean flag = activiteService.updateActivite(activite);
        if (!flag){
            return R.fail("修改失败");
        }
        return R.ok();
    }

    /**
     *  删除活动
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R deleteActivite(@PathVariable Long id){
        log.info("删除活动: {}",id);
        Boolean flag = activiteService.removeById(id);
        if (!flag){
            return R.fail("删除失败");
        }
        return R.ok();
    }

    @GetMapping("/listselect/{channel}")
    public R getActiviteListByChannel(@PathVariable String channel){
        List<Activite> list = activiteService.list(Wrappers.<Activite>lambdaQuery().eq(Activite::getChannel, channel)
                .eq(Activite::getStatus, 2));
        return R.ok(list);
    }



}
